﻿namespace Com.Lancetinc.Evs.Communications
{
    public class InstrumentStateQueryer
    {
        private readonly IInstrumentManager instManager;
        private long _tickTimes = 0;

        public InstrumentStateQueryer(IInstrumentManager instManager)
        {
            this.instManager = instManager;
        }

        public void ClearTickTimes()
        {
            _tickTimes = -1; // 设置次数为-1，以便下次连上时马上就能查询
        }

        public void QueryIfNeed()
        {
            if (!instManager.IsOnline)
            {
                return;
            }

            // 2秒钟发一次，下位机用此命令计时
            _tickTimes++;
            // 没取得序列号时每秒发送一次，取得后2秒发送一次
            if (string.IsNullOrEmpty(instManager.Instrument.HcSerialNo) ||
                _tickTimes % 2 == 0)
            {
                instManager.Send(PacketHelper.CreateState());
            }
        }
    }
}
